package ru.urfc.portal.ui.client.googlets.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.AnchorElement;
import com.google.gwt.dom.client.Element;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Widget;

import ru.urfc.portal.ui.client.BaseGooglet;
import ru.urfc.portal.ui.client.Style;
import ru.urfc.portal.ui.client.descriptor.CompositeGridField;
import ru.urfc.portal.ui.client.descriptor.GridDescriptor;
import ru.urfc.portal.ui.client.model.catalogs.NetLicenseState;
import ru.urfc.portal.ui.client.model.catalogs.NetState;
import ru.urfc.portal.ui.client.renderers.AzimuthRenderer;
import ru.urfc.portal.ui.client.renderers.CoordinatesRenderer;
import ru.urfc.portal.ui.client.renderers.FrequenciesListRenderer;

public class DeviceInfo extends BaseGooglet {

	interface Binder extends UiBinder<Widget, DeviceInfo> {}
	private static Binder binder = GWT.create(Binder.class);
	
	@UiField Style style;
	@UiField Element name;
	@UiField Element number;
	@UiField Element watts;
	@UiField Element realization;
	@UiField Element nChannels;
	@UiField Element coordinates;
	@UiField Element address;
//	@UiField AnchorElement prices;
	@UiField Element price;
	@UiField Element telegraphCallsign;
	@UiField Element telephoneCallsign;
	
	@UiField AnchorElement netAnchor;
	@UiField Element netState;
	@UiField Element netLicenseNumber;
	@UiField Element netLicenseIssueDate;
	@UiField Element netLicenseExpireDate;
	@UiField Element netLicenseState;

	@UiField Element deviceAndSectors;
	@UiField Element sectorTemplate;
	@UiField Element antennaType;
	@UiField Element polarization;
	@UiField Element height;
	@UiField Element azimuth;
	@UiField Element rayClasses;
	@UiField Element frequenciesIn;
	@UiField Element frequenciesOut;

	@UiField Element certificates;
	@UiField Element certificateTemplate;
	@UiField Element certificateNumber;
	@UiField Element certificateIssueDate;
	@UiField Element certificateExpireDate;

//	@UiField Element measurements;
//	@UiField Element measurementTemplate;
//	@UiField Element measurementNumber;
//	@UiField Element measurementIssueDate;
	
	@Override
	protected void init() {
		initWidget(binder.createAndBindUi(this));
//		prices.setHref(URFC_PRICES);
		
		GridDescriptor descriptor = new GridDescriptor(DEVICE, CLIENTS, getClientId(), DEVICES, getItemId());
		descriptor.addTextField("type/name", name);
		descriptor.addTextField("number", number);
		descriptor.addIntField("watts", watts).getIntRenderer().setWattsFormat();
		descriptor.addTextField("realization/name", realization);
		descriptor.addTextField("nChannels", nChannels);
		CompositeGridField coordinatesField = descriptor.addCompositeField();
		coordinatesField.addField(descriptor.addField("latitude", Double.class));
		coordinatesField.addField(descriptor.addField("longitude", Double.class));
		descriptor.assign(new CoordinatesRenderer(coordinatesField, coordinates));
		descriptor.addCompositeField("city", "address").assign(address, ", ");
		descriptor.addMoneyField("price", price);
		descriptor.addCompositeField("telegraphCallsign", "telegraphCallsignNumber").assign(telegraphCallsign);
		descriptor.addCompositeField("telephoneCallsign", "telephoneCallsignNumber").assign(telephoneCallsign);
		createController(descriptor);

		initNet();
		initSectors();
		initCertificates();
//		initMeasurements();
	}

	private void initNet() {
		GridDescriptor descriptor = new GridDescriptor(NET, CLIENTS, getClientId(), DEVICES, getItemId(), DEVICE_NET);
		CompositeGridField netField = descriptor.addCompositeField("name", "region").assign(null, ", ");
		descriptor.addAnchorFields(netField, "id", PAGE_NET_DETAILS, netAnchor, netAnchor);
		descriptor.addCIFields("state/text", "state/code", NetState.class, netState, style);
		descriptor.setAddFieldPathPrefix("currentLicense");
		descriptor.addTextField("number", netLicenseNumber);
		descriptor.addDateField("issueDate", netLicenseIssueDate);
		descriptor.addDateField("expireDate", netLicenseExpireDate);
		descriptor.addCIFields("state/text", "state/code", NetLicenseState.class, netLicenseState, style);
		createController(descriptor);
	}

	private void initSectors() {
		GridDescriptor descriptor = new GridDescriptor(SECTOR, 
				CLIENTS, getClientId(), DEVICES, getItemId(), DEVICE_SECTORS);		
		descriptor.addTextField("antennaType/text", antennaType);
		descriptor.addTextField("polarization/text", polarization);
		descriptor.addDblField("height", height).getDblRenderer().setMetersFormat();
		CompositeGridField azimuthField = descriptor.addCompositeField();
		azimuthField.addField(descriptor.addField("azimuthMinDegrees", Double.class));
		azimuthField.addField(descriptor.addField("azimuthMinMinutes", Integer.class));
		azimuthField.addField(descriptor.addField("azimuthMaxDegrees", Double.class));
		azimuthField.addField(descriptor.addField("azimuthMaxMinutes", Integer.class));
		descriptor.assign(new AzimuthRenderer(azimuthField, azimuth));
		descriptor.addTextField("rayClasses", rayClasses);
		descriptor.assign(new FrequenciesListRenderer(
				descriptor.addField("frequenciesIn"), frequenciesIn)); 
		descriptor.assign(new FrequenciesListRenderer(
				descriptor.addField("frequenciesOut"), frequenciesOut)); 
		createController(descriptor, deviceAndSectors, sectorTemplate);
	}

	private void initCertificates() {
		GridDescriptor descriptor = new GridDescriptor(CERTIFICATE, 
				CLIENTS, getClientId(), DEVICES, getItemId(), DEVICE_CRETIFICATES);
		descriptor.addCompositeField("seria", "number").assign(certificateNumber, " №");
		descriptor.addDateField("issueDate", certificateIssueDate);
		descriptor.addDateField("expireDate", certificateExpireDate);
		createController(descriptor, certificates, certificateTemplate);
	}

//	private void initMeasurements() {
//		GridDescriptor descriptor = new GridDescriptor(RECORD, 
//				CLIENTS, getClientId(), DEVICES, getItemId(), DEVICE_MEASURENENTS);
//		descriptor.addTextField("number", measurementNumber);
//		descriptor.addDateField("issueDate", measurementIssueDate);
//		createController(descriptor, measurements, measurementTemplate);
//	}

}